<%@ include file="shared_header.jsp" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="vrp.SaleOrder" %>
<%@ page import="vrp.Partner" %>
<%@ page import="vrp.Customer" %>
<%@ page import="vrp.Product" %>

<%
	SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
	PersistenceManager pm = PMF.get().getPersistenceManager();
	String query = "";
	
    query = "select from " + Partner.class.getName() + " where type==0 order by name asc";
    List<Partner> partners = (List<Partner>) pm.newQuery(query).execute();
%>

<script type="text/javascript" src="/scripts/saleorders.js"></script>

<%
	String[] saleOrderStatusValue = {Integer.toString(0),Integer.toString(1)};
	String[] saleOrderStatusDisplay = {"Processing","Finished"};

	String[] recurringValue = {Integer.toString(0),Integer.toString(1),Integer.toString(2),Integer.toString(3),Integer.toString(7),Integer.toString(14)};
	String[] recurringDisplay = {"One-off","Every Day","Every 2 Days","Every 3 Days","Every 7 Days","Every 14 Days"};
	
    String error = request.getParameter("error");
    if (error != null) {
    	if (error.equals("quantity")) {
%>
    		<div class="ui-state-error">Sale order quantity must be a positive number.</div>
<%
    	}
    	if (error.equals("recurringType")) {
%>
    		<div class="ui-state-error">The specified recurring type is invalid.</div>
<%
    	}
    	if (error.equals("recurringDate")) {
%>
    		<div class="ui-state-error">The recurring end date cannot be before the start date.</div>
<%
    	}
    }

    List<SaleOrder> saleOrders;
    List<Customer> customers;
    List<Product> products;	
	
	String partnerIDStr = request.getParameter("client");
	long partnerID = 0;
	if (partnerIDStr != null && !partnerIDStr.equals("") && !partnerIDStr.equals("0")) {
		partnerID = Integer.parseInt(partnerIDStr);
	}
%>
	<div style="padding: 0px 10px;">
		<h2>List of Sale Orders</h2>
		<div>
			<form target="_self" action="/saleorders.jsp">
				Client:&nbsp;
    			<select name="client">
<%
    				for (Partner p : partners) {
%>			    			
				 	<option value="<%= p.getKey().getId() %>" <%= ((partnerID == p.getKey().getId())?"selected":"") %>><%= p.getName() %></option>
<%
       			}
%>							
				</select>
				<input type="Submit" value="Select" />
	    	</form>
		</div>	
		<br/>	
<%
	if (partnerIDStr != null && !partnerIDStr.equals("") && !partnerIDStr.equals("0")) {
		Key partnerKey = KeyFactory.createKey(Partner.class.getSimpleName(), partnerID);
		query = "select from " + SaleOrder.class.getName() + " where partner == partnerParam parameters String partnerParam order by customer asc, product asc";
		saleOrders = (List<SaleOrder>) pm.newQuery(query).execute(partnerKey);
		query = "select from " + Customer.class.getName() + " where partner == partnerParam parameters String partnerParam order by name asc";
		customers = (List<Customer>) pm.newQuery(query).execute(partnerKey);
		query = "select from " + Product.class.getName() + " where partner == partnerParam parameters String partnerParam order by name asc";
		products = (List<Product>) pm.newQuery(query).execute(partnerKey);
%>
		<table style="width: 100%;">
<%
    
    	if (saleOrders.isEmpty()) {
%>
			<tr><td>There is no sale order in the database.</td></tr>
<%
    	} else {
%>
			<tr>
				<th>Customer</th>
				<th>Product</th>
				<th>Quantity</th>
				<th>Recurring Type</th>
				<th>Start Date</th>
				<th>End Date</th>
				<th>Next Delivery Date</th>
				<th>Status</th>
				<th>Action</th>
			</tr>
<%
        	for (SaleOrder o : saleOrders) {
%>
			<tr>
				<td style="text-align: left;"><%= (pm.getObjectById(Customer.class, o.getCustomer())).getName() %></td>
				<td style="text-align: left;"><%= (pm.getObjectById(Product.class, o.getProduct())).getName() %></td>
				<td style="text-align: right;"><%= o.getQuantity() %></td>
				<td style="text-align: center;"><%= writeSwitch(Integer.toString(o.getRecurringType()),recurringValue,recurringDisplay) %></td>
				<td style="text-align: center;"><%= sdf.format(o.getStartDate()) %></td>
				<td style="text-align: center;"><%= sdf.format(o.getEndDate()) %></td>
				<td style="text-align: center;"><%= sdf.format(o.getNextDeliveryDate()) %></td>
				<td style="text-align: center;"><%= writeSwitch(Integer.toString(o.getStatus()),saleOrderStatusValue,saleOrderStatusDisplay) %></td>
				<td style="text-align:center;">
					<form action="/deliveries.jsp" method="get" target="_blank">
						<input type="hidden" name="saleOrder" value="<%= o.getKey().getId() %>"/>
						<input type="submit" value="View Deliveries"/>
					</form>
<%
				if (o.getStatus() == 0) { //PROCESSING
%>					
					<form action="/adddelivery" method="post">
						<input type="hidden" name="saleOrder" value="<%= o.getKey().getId() %>"/>
						<input type="hidden" name="client" value="<%= partnerID %>"/>
						<input type="submit" value="Send to Delivery"/>
					</form>
<%
				}
%>
					<input type="button" value="Edit" class="buttonedit"/>
					<input type="button" value="Delete" class="buttondelete"/>
				</td>
			</tr>
			<tr style="display: none;">
				<td colspan="9">
					<form action="/editsaleorder" method="post" id="formedit">
						<div><b>Edit Sale Order</b></div>
			    		<div>Customer:&nbsp;
			    			<select name="customer">
<%
        					for (Customer c : customers) {
%>			    			
							 	<option value="<%= c.getKey().getId() %>" <%= (c.getKey().getId()==o.getCustomer().getId())?"selected":"" %>><%= c.getName() %></option>
<%
        					}
%>							</select>
			    		</div>
			    		<div>Product:&nbsp;
			    			<select name="product">
<%
        					for (Product p : products) {
%>			    			
							 	<option value="<%= p.getKey().getId() %>" <%= (p.getKey().getId()==o.getProduct().getId())?"selected":"" %>><%= p.getName() %></option>
<%
        					}
%>							</select>
			    		</div>
			    		<div>Quantity:&nbsp;<input type="text" name="quantity" value="<%= o.getQuantity() %>" /></div>
			    		<div>Recurring Type:&nbsp;<%= writeSelect("recurringType",Integer.toString(o.getRecurringType()),recurringValue,recurringDisplay) %></div>
			    		<div>Start Date:&nbsp;<input type="text" name="startDate" value="<%= sdf.format(o.getStartDate()) %>" class="datePicker" /></div>
			    		<div>End Date:&nbsp;<input type="text" name="endDate" value="<%= sdf.format(o.getEndDate()) %>" class="datePicker" /></div>
			    		<input type="hidden" name="saleOrder" value="<%= o.getKey().getId() %>"/>
			    		<input type="hidden" name="client" value="<%= partnerID %>"/>
						<input type="submit" value="Edit" />
						<input type="button" value="Cancel" class="buttoncancel" />
					</form>
				</td>
			</tr>
			<tr style="display: none;">
				<td colspan="9">
					<form action="/deletesaleorder" method="post" id="formdelete">
						<div><b>Delete Sale Order</b></div>
			    		<div>This action would delete the above sale order permanently. Proceed?</div>
						<input type="hidden" name="saleOrder" value="<%= o.getKey().getId() %>"/>
						<input type="hidden" name="client" value="<%= partnerID %>"/>
						<input type="submit" value="Delete" />
						<input type="button" value="Cancel" class="buttoncancel" />
					</form>
				</td>
			</tr>
<%
        	}
    	}
%>
			
		</table>
		<input type="button" value="Add Sale Order" class="buttonadd" />
  		<div style="display: none;">
			<form action="/addsaleorder" method="post" id="formadd">
				<input type="hidden" name="client" value="<%= partnerID %>"/>
	    		<div>Customer:&nbsp;
	    			<select name="customer">
<%
     				for (Customer c : customers) {
%>			    			
					 	<option value="<%= c.getKey().getId() %>"><%= c.getName() %></option>
<%
        			}
%>							
					</select>
	    		</div>
	    		<div>Product:&nbsp;
	    			<select name="product">
<%
     				for (Product p : products) {
%>			    			
					 	<option value="<%= p.getKey().getId() %>"><%= p.getName() %></option>
<%
        			}
%>							
					</select>
	    		</div>
	    		<div>Quantity:&nbsp;<input type="text" name="quantity" /></div>
	    		<div>Recurring Type:&nbsp;<%= writeSelect("recurringType","",recurringValue,recurringDisplay) %></div>
	    		<div>Start Date:&nbsp;<input type="text" name="startDate" class="datePicker" /></div>
	    		<div>End Date:&nbsp;<input type="text" name="endDate" class="datePicker" /></div>
	    		<div><input type="submit" value="Add" /><input type="button" value="Cancel" class="buttoncancel" /></div>
	  		</form>
	  	</div>
<%
	}
%>
	</div>

<%
    pm.close();
%>

<%@ include file="shared_footer.jsp" %>